home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / bin / check-language-support < prev    next >
Text File  |  2009-10-23  |  12KB  |  271 lines

  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import os
  5. import gettext
  6. import apt
  7. import re
  8.  
  9. from LanguageSelector.LanguageSelector import *
  10.  
  11. from optparse import OptionParser
  12. from gettext import gettext as _
  13.  
  14. class checkLanguageSupport(LanguageSelectorBase, apt.Cache):
  15.  
  16.     def getMissingPackages(self, pkgcode):
  17.         if (self.system_pkgcode and pkgcode == self.system_pkgcode):
  18.             scanlist = ['language-pack', 'language-support-fonts', 'language-support-input', 'language-support-writing']
  19.         else:
  20.             scanlist = ['language-pack']
  21.         for x in scanlist:
  22.             pkg = '%s-%s' % (x, pkgcode)
  23.             if pkg in self:
  24.                 if not self[pkg].isInstalled and \
  25.                    not self[pkg].markedInstall:
  26.                     self.missing.add(pkg)
  27.                 else:
  28.                     self.installed.add(pkg)
  29.         if pkgcode in self.pkg_translations:
  30.             for (pkg, translation) in self.pkg_translations[pkgcode]:
  31.                 if pkg in self and \
  32.                     (self[pkg].isInstalled or \
  33.                     self[pkg].markedInstall):
  34.                     if not self[translation].isInstalled and \
  35.                        not self[translation].markedInstall:
  36.                         self.missing.add(translation)
  37.                     else:
  38.                         self.installed.add(translation)
  39.                     
  40.         if pkgcode in self.pkg_writing and \
  41.            (pkgcode == self.system_pkgcode or \
  42.            self['language-support-writing-%s' % pkgcode].isInstalled or \
  43.            self['language-support-writing-%s' % pkgcode].markInstall):
  44.             for (pkg, pull_pkg) in self.pkg_writing[pkgcode]:
  45.                 if '|' in pkg:
  46.                     # multiple dependencies, if one of them is installed, pull the pull_pkg
  47.                     added = 0
  48.                     for p in pkg.split('|'):
  49.                         if p in self and \
  50.                             (self[p].isInstalled  or \
  51.                             self[p].markedInstall) and \
  52.                             pull_pkg in self and \
  53.                             added == 0:
  54.                             if not self[pull_pkg].isInstalled and \
  55.                                not self[pull_pkg].markedInstall:
  56.                                 self.missing.add(pull_pkg)
  57.                             else:
  58.                                 self.installed.add(pull_pkg)
  59.                             added = 1
  60.                 else:
  61.                     if pkg in self and \
  62.                         (self[pkg].isInstalled or \
  63.                         self[pkg].markedInstall) and \
  64.                         pull_pkg in self:
  65.                         if not self[pull_pkg].isInstalled and \
  66.                            not self[pull_pkg].markedInstall:
  67.                             self.missing.add(pull_pkg)
  68.                         else:
  69.                             self.installed.add(pull_pkg)
  70.  
  71.     def __init__(self, options):
  72.         self.BLACKLIST = os.path.join(options.datadir, 'data', 'blacklist')
  73.         self.LANGCODE_TO_LOCALE = os.path.join(options.datadir, 'data', 'langcode2locale')
  74.         self.PACKAGE_DEPENDS = os.path.join(options.datadir, 'data', 'pkg_depends')
  75.  
  76.         LanguageSelectorBase.__init__(self, options.datadir)
  77.         apt.Cache.__init__(self)
  78.         if self._depcache.BrokenCount > 0:
  79.             print ("%s\n%s" % (
  80.                 _("Software database is broken"),
  81.                 _("It is impossible to install or remove any software. "
  82.                   "Please use the package manager \"Synaptic\" or run "
  83.                   "\"sudo apt-get install -f\" in a terminal to fix "
  84.                   "this issue at first.")))
  85.             sys.exit(1)
  86.         
  87.         self.langpack_locales = {}
  88.         self.pkg_translations = {}
  89.         self.pkg_writing = {}
  90.         filter_list = {}
  91.         blacklist = []
  92.         self.missing = set()
  93.         self.installed = set()
  94.         self.system_pkgcode = ''
  95.         
  96.         for l in open(self.BLACKLIST):
  97.             l = l.strip()
  98.             if not l.startswith('#'):
  99.                 blacklist.append(l)
  100.         
  101.         for l in open(self.LANGCODE_TO_LOCALE):
  102.             try:
  103.                 l = l.rstrip()
  104.                 if ':' in l:
  105.                     (pkgcode, locale) = l.split(':')
  106.                 else:
  107.                     pkgcode = l
  108.                     locale = l
  109.             except ValueError:
  110.                 continue
  111.             self.langpack_locales[locale] = pkgcode
  112.         
  113.         for l in open(self.PACKAGE_DEPENDS):
  114.             if l.startswith('#'):
  115.                 continue
  116.             try:
  117.                 l = l.rstrip()
  118.                 # sort out comments
  119.                 if l.find('#') >= 0:
  120.                     continue
  121.                 (c, lc, k, v) = l.split(':')
  122.             except ValueError:
  123.                 continue
  124.             if (c == 'tr' and lc == ''):
  125.                 filter_list[v] = k
  126.             elif (c == 'wa' and lc != ''):
  127.                 if not lc in self.pkg_writing:
  128.                     self.pkg_writing[lc] = []
  129.                 self.pkg_writing[lc].append(("%s" % k, "%s" % v))
  130.  
  131.         # get list of all packages available on the system and filter them
  132.         for item in self.keys():
  133.             if item in blacklist: 
  134.                 continue
  135.             for x in filter_list.keys():
  136.                 if item.startswith(x) and not item.endswith('-base'):
  137.                     # parse language code
  138.                     langcode = item.replace(x, '')
  139.                     #print "%s\t%s" % (item, langcode)
  140.                     if langcode == 'zh':
  141.                         # special case: zh langpack split
  142.                         for langcode in ['zh-hans', 'zh-hant']:
  143.                             if not langcode in self.pkg_translations:
  144.                                 self.pkg_translations[langcode] = []
  145.                             self.pkg_translations[langcode].append(("%s" % filter_list[x], "%s" % item))
  146.                     elif langcode in self.langpack_locales.values():
  147.                         # langcode == pkgcode
  148.                         if not langcode in self.pkg_translations:
  149.                             self.pkg_translations[langcode] = []
  150.                         self.pkg_translations[langcode].append(("%s" % filter_list[x], "%s" % item))
  151.                         #print self.pkg_translations[langcode]
  152.                     else:
  153.                         # need to scan for LL-CC and LL-VARIANT codes
  154.                         for locale in self.langpack_locales.keys():
  155.                             if '_' in locale or '@' in locale:
  156.                                 if '@' in locale:
  157.                                     (locale, variant) = locale.split('@')
  158.                                 else:
  159.                                     variant = ''
  160.                                 (lcode, ccode) = locale.split('_')
  161.                                 if langcode in ["%s-%s" % (lcode, ccode.lower()),
  162.                                                 "%s%s" % (lcode, ccode.lower()),
  163.                                                 "%s-%s" % (lcode, variant),
  164.                                                 "%s%s" % (lcode, variant),
  165.                                                 "%s-latn" % lcode,
  166.                                                 "%slatn" % lcode,
  167.                                                 "%s-%s-%s" % (lcode, ccode.lower(), variant),
  168.                                                 "%s%s%s" % (lcode, ccode.lower(), variant)]:
  169.                                     # match found, get matching pkgcode
  170.                                     langcode = self.langpack_locales[locale]
  171.                                     if not langcode in self.pkg_translations:
  172.                                         self.pkg_translations[langcode] = []
  173.                                     self.pkg_translations[langcode].append(("%s" % filter_list[x], "%s" % item))
  174.                                     #print self.pkg_translations[langcode]
  175.                                     break
  176.  
  177.         if options.language:
  178.             pkgcode = ''
  179.             if options.language == 'zh-hans' or options.language == 'zh-hant':
  180.                 self.system_pkgcode = options.language
  181.             elif options.language in self.langpack_locales:
  182.                 self.system_pkgcode = self.langpack_locales[options.language]
  183.             else:
  184.                 # pkgcode = ll
  185.                 if '_' in options.language:
  186.                     (self.system_pkgcode) = options.language.split('_')[0]
  187.                 elif '@' in options.language:
  188.                     (self.system_pkgcode) = options.language.split('@')[0]
  189.                 else:
  190.                     self.system_pkgcode = options.language
  191.  
  192.             self.getMissingPackages(self.system_pkgcode)
  193.             
  194.         elif options.all:
  195.             # try all available languages
  196.             pkgcodes = []
  197.             for item in self.keys():
  198.                 if item in blacklist:
  199.                     continue
  200.                 if item.startswith('language-pack-') and \
  201.                    not item.startswith('language-pack-gnome') and \
  202.                    not item.startswith('language-pack-kde') and \
  203.                    not item.endswith('-base'):
  204.                     pkgcode = item.replace('language-pack-', '')
  205.                     pkgcodes.append(pkgcode)
  206.  
  207.             for pkgcode in pkgcodes:
  208.                 self.getMissingPackages(pkgcode)
  209.  
  210.         else:
  211.             # get a list of language-packs we have already installed or are going to install
  212.             # 1. system locale
  213.             system_langcode = self.getSystemDefaultLanguage()
  214.             if system_langcode == None:
  215.                 system_langcode = 'en_US'
  216.                 if system_langcode in self.langpack_locales:
  217.                     self.system_pkgcode = self.langpack_locales[system_langcode]
  218.             # 2. installed language-packs
  219.             pkgcodes = []
  220.             for item in self.keys():
  221.                 if item in blacklist: 
  222.                     continue
  223.                 if item.startswith('language-pack-') and \
  224.                    not item.startswith('language-pack-gnome') and \
  225.                    not item.startswith('language-pack-kde') and \
  226.                    not item.endswith('-base') and \
  227.                    (self[item].isInstalled or \
  228.                    self[item].markedInstall):
  229.                     pkgcode = item.replace('language-pack-', '')
  230.                     pkgcodes.append(pkgcode)
  231.             if self.system_pkgcode and \
  232.                not self.system_pkgcode in pkgcodes:
  233.                 pkgcodes.append(self.system_pkgcode)
  234.             
  235.             for pkgcode in pkgcodes:
  236.                 self.getMissingPackages(pkgcode)
  237.                     
  238.         if options.show_installed:
  239.             show = self.missing | self.installed
  240.         else:
  241.             show = self.missing
  242.         if show:
  243.             print (' '.join(sorted(show)))
  244.  
  245. if __name__ == "__main__":
  246.     parser = OptionParser()
  247.     parser.add_option("-l", "--language",
  248.               help=_("target language code"))
  249.     parser.add_option("-d", "--datadir",
  250.               default="/usr/share/language-selector/",
  251.               help=_("alternative datadir"))
  252.     parser.add_option("-a", "--all", action='store_true', default=False,
  253.               help=_("check all available languages"))
  254.     parser.add_option("--show-installed",
  255.               action='store_true', default=False,
  256.               help=_("show installed packages as well as missing ones"))
  257.     (options, args) = parser.parse_args()
  258.  
  259. # sanity check for language code
  260. if options.language and \
  261.    not re.match('^([a-z]{2,3}(_[A-Z]{2})?(@[a-z]+)?|(zh-han[st]))$', options.language):
  262.     print ("Error: Unsupported language code format.\n\
  263. Supported formats: 'll' or 'lll'\n\
  264.                    'll_CC' or 'lll_CC'\n\
  265.                    'll@variant' or 'lll@variant'\n\
  266.                    'll_CC@variant' or 'lll_CC@variant'\n\
  267. Also supported are 'zh-hans' and 'zh-hant'.")
  268.     sys.exit(1)
  269. else:
  270.     checkLanguageSupport(options)
  271.